package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.B;
import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.CommentOfComment;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.CommentOfCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private CommentOfCommentMapper commentOfCommentMapper;
    @Override
    public void save(Comment comment, String imgUrl) {
        commentMapper.insert(comment);
        if (imgUrl.length() > 10){
            String[] urls = imgUrl.split("&");
            for (int i = 1; i < urls.length; i++) {
                commentMapper.saveImg(comment.getId(),urls[i]);
            }
        }
    }

    @Override
    public PageInfo selectStrategyByStrategyId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Comment> list = commentMapper.selectStrategyByStrategyId(qo);
        for (Comment comment : list) {
            List<String> urls = commentMapper.selectImgUrl(comment.getId());
            LinkedList<B> b = new LinkedList<B>();
            for (String url : urls) {
                b.add(new B(url));
            }
            comment.setImgUrls(b);
        }
        return new PageInfo(list);
    }

    @Override
    public Comment selectById(Long id) {
        Comment comment = commentMapper.selectByPrimaryKey(id);
        List<String> urls = commentMapper.selectImgUrl(id);
        Strategy strategy = strategyMapper.selectByPrimaryKey(comment.getStrategyId());
        comment.setStrategyTitle(strategy.getTitle());
        comment.setStrategyImg(strategy.getCoverUrl());
        LinkedList<B> b = new LinkedList<B>();
        for (String url : urls) {
            b.add(new B(url));
        }
        comment.setImgUrls(b);
        return comment;
    }

    @Override
    public void saveCommentOfComment(CommentOfComment commentOfComment) {
        commentOfComment.setTime(new Date());
        commentOfCommentMapper.insert(commentOfComment);
    }

    @Override
    public PageInfo selectCommentOfCommentByCommentId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"time desc");
        List<CommentOfComment> list = commentOfCommentMapper.queryForListByCommentId(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo selectByUserId(CommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"time desc");
        List<Comment> list = commentMapper.selectByUserId(qo);
        for (Comment comment : list) {
            List<String> urls = commentMapper.selectImgUrl(comment.getId());
            Strategy strategy = strategyMapper.selectByPrimaryKey(comment.getStrategyId());
            comment.setStrategyTitle(strategy.getTitle());
            comment.setStrategyImg(strategy.getCoverUrl());
            LinkedList<B> b = new LinkedList<B>();
            for (String url : urls) {
                b.add(new B(url));
            }
            comment.setImgUrls(b);
        }
        return new PageInfo(list);
    }
}
